package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;

import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;

    @Autowired
    FlavorMapper flavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    /**
     * 新增菜品和对应的口味
     * @param dishDTO
     */
    @Override
    @Transactional//事务注解
    public void saveWithFlavor(DishDTO dishDTO) {
        //向菜品表插入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        //向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));//遍历list，设置id
            flavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.getAll(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //判断是否有菜品在售卖
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断套餐中有没有这个菜品
        Integer count = setmealDishMapper.countByDishId(ids);
        if (count > 0 && count != null) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //根据菜品id集合批量删除
        dishMapper.deleteBatch(ids);
        flavorMapper.deleteByDishIds(ids);
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //更换bean对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //更新菜品表
        dishMapper.update(dish);
        //删除口味
        flavorMapper.deleteByDishId(dishDTO.getId());
        //添加口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));//遍历list，设置id
            flavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public DishVO getById(Long id) {
        Dish byId = dishMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(byId,dishVO);
        return dishVO;
    }

    @Override
    public void updateStatusById(Integer status,Long id) {
        Dish dish = dishMapper.getById(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> getCategoryById(Long categoryId) {
        List<DishVO> list = dishMapper.getCategoryById(categoryId);
        return list;
    }

    @Override
    public List<DishItemVO> getSetmealByDishId(Long id) {
        List<DishItemVO> list = dishMapper.getSetmealByDishId(id);
        return list;
    }

    @Override
    public List<DishVO> getCategoryByIdWithFlavor(Dish dish) {
        List<Dish> list = dishMapper.getCategoryByIdAndStatus(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish dish1 : list) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish1,dishVO);
            List<DishFlavor> flavors = flavorMapper.getByDishId(dish1.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
